package cn.ncist.controller;

import cn.ncist.common.RespResult;
import cn.ncist.constant.MessageConstant;
import cn.ncist.health_interface.service.EmailService;
import cn.ncist.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;

@Slf4j
@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {

    @DubboReference
    private EmailService emailService;

    @PostMapping("/send4Order")
    public RespResult send4Order(String telephone){
        log.info("邮箱为：{}",telephone);
        //生成验证码
        Integer validateCode = ValidateCodeUtils.generateValidateCode(4);
        try {
            emailService.sendHtmlEmail(telephone,validateCode);

            return new RespResult(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (MessagingException e) {
            e.printStackTrace();
            return new RespResult(true, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }

    @PostMapping("/send4Login")
    public RespResult send4Login(String telephone){
        log.info("登录的邮箱为：{}",telephone);
        Integer validateCode = ValidateCodeUtils.generateValidateCode(6);
        try {
            emailService.sendHtmlEmail(telephone,validateCode);
            return new RespResult(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (MessagingException e) {
            e.printStackTrace();
            return new RespResult(true,MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }
}
